﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using GameScoring.DBModel;

namespace GameScoring.ViewModel
{
    public class MainPageModel : ObservableObject
    {
        /// <summary>
        /// 回合
        /// </summary>
        private ObservableCollection<MainPageModelItem> _item;
        /// <summary>
        /// 回合
        /// </summary>
        public ObservableCollection<MainPageModelItem> item { get => _item; set => SetProperty(ref _item, value); }
        DBDatabase dBDatabase { get; set; }
        public event Action<string> EventOpen;
        public MainPageModel()
        {
            dBDatabase = new DBDatabase();
            dBDatabase.Init();
            item = new ObservableCollection<MainPageModelItem>();


        }
        public async void LoadData()
        {
            item.Clear();
            var sql = dBDatabase.GetDBConnection();
            var games = await sql.Table<DBGameName>().OrderByDescending(x=>x.Time).ToListAsync();
            foreach (var game in games)
            {
                MainPageModelItem it = new MainPageModelItem()
                {
                    ItemName= game.GameNameStr,
                    createTime = DateTime.FromFileTime(game.Time),
                    ItemId=game.GuidId
                };
                it.EventOpen += It_EventOpen;
                item.Add(it);
            }
        }

        private void It_EventOpen(string par)
        {
            EventOpen?.Invoke(par);
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class MainPageModelItem
    {
        /// <summary>
        /// 
        /// </summary>
        public string ItemName { get; set; }
        public DateTime createTime { get; set; }
        public string ItemId { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public ICommand Open { get; set; }

        public event Action<string> EventOpen;
        public MainPageModelItem()
        {
            Open = new RelayCommand(OpenMethod);
        }

        private void OpenMethod()
        {
            EventOpen?.Invoke(ItemId);
        }
    }
}
